<?php
namespace app\admin\controller;

class Nav extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','a.title','like',"%{$d['keyword']}%"]; }
            if($d['cat_id']!=''){ $map[] = ['and','a.cat_id','=',$d['cat_id']]; }
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];

            $w  = where($map);
            $rs = $this->db->query("select a.*,b.cat_title ptitle from nav a left join nav_cat b on a.cat_id = b.id $w[0] order by a.sort desc,a.id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'link_type'=>function($v){return c("type.link_type.$v");},
                    'create_time' => 'com_time_format',
                    'status'=>function($v){
                        return $v==0?'显示':'隐藏';
                    },
                ];
                $data = com_format($rs[0],$rule);
                return suc($data, $rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        $cat = $this->db->query("select cat_title,id from nav_cat order by sort desc,id desc");
        return $this->fetch(['cat'=>$cat]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'cat_id'=>'require',
                'title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            unset($d['file']);
            $id = $this->db->insert('nav',$d);
            m('Attachment','xcom')->imgSure('nav',$id,$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $cat = $this->db->query("select cat_title,id from nav_cat order by sort desc,id desc");
        return $this->fetch(['cat'=>$cat]);
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'cat_id'=>'require',
                'title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            unset($d['file']);
            $id = $this->db->update('nav',$d);
            m('Attachment','xcom')->imgSure('nav',$d['id'],$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }

        $cat = $this->db->query("select cat_title,id from nav_cat order by sort desc,id desc");
        $id = $this->get['id'];
        $info = $this->db->find('nav',$id);
        return $this->fetch(['cat'=>$cat,'info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('nav',$d['id']);
            m('Attachment','xcom')->imgDel('nav',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('nav',$id);
                m('Attachment','xcom')->imgDel('nav',$id,true);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }

    //详情
    public function detail($id){
        return $this->fetch();
    }
}
